<?php

namespace yan\backendclient\modules\backendext\controllers;

use Yii;
use yan\backendclient\modules\backendext\models\BackendExtensions;
use yii\data\ActiveDataProvider;
use yan\backendclient\controllers\DebugController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * DefaultController implements the CRUD actions for BackendExtensions model.
 */
class DefaultController extends DebugController
{
    
    /**
     * Lists all BackendExtensions models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model = new BackendExtensions;

        $dataProvider = new ActiveDataProvider([
            'query' => BackendExtensions::find(),
        ]);

        return $this->render('index', [
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single BackendExtensions model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new BackendExtensions model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new BackendExtensions();

        $data = $model->loadExtensions();
        
        $namespaceList = [];
        foreach ((array)$data as $key => $val) {
            $namespaceList[$val['class']] = $val['class'];
        }
        $list['dropDownClass'] = $namespaceList;

        $model->loadDefaultValues();

        $list['urlReferrer'] = \Yii::$app->request->referrer;

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $list['urlReferrer'] = Yii::$app->request->post('urlReferrer');
            if( $model->save() ){
                Yii::$app->getSession()->setFlash('message', Yii::t('debug', 'Successful'));
                return strpos($list['urlReferrer'], $this->action->id)==0 ? $this->redirect($list['urlReferrer']) : $this->redirect(['index']);
            }
            
        }

        return $this->render('create', [
            'model' => $model,
            'list' => $list,
        ]);
    }

    /**
     * Updates an existing BackendExtensions model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        $data = $model->loadExtensions();
        
        $namespaceList = [];
        foreach ((array)$data as $key => $val) {
            $namespaceList[$val['class']] = $val['class'];
        }
        $list['dropDownClass'] = $namespaceList;
        
        
        $list['urlReferrer'] = \Yii::$app->request->referrer;

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $list['urlReferrer'] = Yii::$app->request->post('urlReferrer');
            if( $model->save() ){
                Yii::$app->getSession()->setFlash('message', Yii::t('debug', 'Successful'));
                return strpos($list['urlReferrer'], $this->action->id)==0 ? $this->redirect($list['urlReferrer']) : $this->redirect(['index']);
            }
            
        }

        return $this->render('update', [
            'model' => $model,
            'list' => $list,
        ]);
    }

    /**
     * Deletes an existing BackendExtensions model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the BackendExtensions model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return BackendExtensions the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = BackendExtensions::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
